package com.qiall.omsserver.controller;

import com.qiall.bashutil.OsScriptUtil;

import com.qiall.common.exception.BusinessException;
import com.qiall.common.exception.BusinessResult;
import com.qiall.common.exception.ResultCode;
import com.qiall.omsserver.data.entity.ApplicationInfo;
import com.qiall.omsserver.data.entity.OmsLogInfo;
import com.qiall.omsserver.data.mapper.ApplicationInfoMapper;
import com.qiall.omsserver.data.mapper.OmsLogInfoMapper;
import com.qiall.omsserver.service.AppOmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("oms/manage")
@Slf4j
public class OmsManageController {

    @Autowired
    private AppOmsService appOmsService;


    @GetMapping("deploy")
    public BusinessResult<Void> deploy(@RequestParam("appId") Integer appId) {
        try {
            appOmsService.deploy(appId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
        return BusinessResult.success(null);
    }

    @Autowired
    private OmsLogInfoMapper omsLogInfoMapper;

    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;

    @GetMapping("getLogList")
    public BusinessResult<List<OmsLogInfo>> getLogList(@RequestParam("appId") Integer appId, @RequestParam("createTime") String createTime) {
        List<OmsLogInfo> infoList = omsLogInfoMapper.selectByAppIdAndDate(appId, createTime);
        return BusinessResult.success(infoList);
    }

    @GetMapping("/applicationInfoList")
    public BusinessResult<List<ApplicationInfo>> getApplicationInfoList() {
        List<ApplicationInfo> applicationInfos = applicationInfoMapper.selectAll();
        return BusinessResult.success(applicationInfos);
    }


}
